﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

namespace Jinndev.UI {

    public class NotificationUI : BaseUIBehaviour {

        public static string PATH = "Jinndev/Prefab/UI/Common/NotificationUI";
        public static uint max = 10;

        public static NotificationUI Create(object message, LogType type = LogType.Log) {
            NotificationUI notificationUI = null;
            if (UIManager.Instance != null) {
                notificationUI = UIManager.Instance.AddTop<NotificationUI>(PATH, 0);
            }
            else {
                GameObject canvas = GameObject.Find("Canvas");
                if (canvas != null) {
                    notificationUI = CommonUtil.Instantiate<NotificationUI>(PATH, canvas);
                }
                else {
                    throw new System.Exception("Canvas not found");
                }
            }
            notificationUI.Init(message, type);
            return notificationUI;
        }
        public static NotificationUI Create(System.Exception exception) {
            return Create(exception.Message, LogType.Exception);
        }

        // 当前所有实例的列表
        private static List<NotificationUI> list = new List<NotificationUI>();
        /// <summary>
        /// 同步列表中所有实例的y坐标
        /// </summary>
        private static void SyncPosition() {
            float y = 0;
            for (int i = list.Count - 1; i >= 0; i--) {
                RectTransform transform = list[i].transform as RectTransform;
                //transform.DOKill();
                //transform.DOAnchorPosY(y, 0.2f);
                Vector2 pos = transform.anchoredPosition;
                pos.y = y;
                transform.anchoredPosition = pos;
                y += transform.sizeDelta.y;
            }
        }


        public Text textMessage;

        private RectTransform rectTransform;
        private float duration = 2.5f;
        private bool destroyed = false;

        private void Awake() {
            rectTransform = transform as RectTransform;
            list.Add(this);
            if (list.Count > max) {
                for (int i = 0; i < list.Count - max; i++) {
                    list[i].duration = 0;
                }
            }
            rectTransform.anchoredPosition = new Vector2(rectTransform.sizeDelta.x, -rectTransform.sizeDelta.y);
        }

        public void Init(object message, LogType type = LogType.Log) {
            string text = message == null ? "Null" : message.ToString();
            if (type == LogType.Warning) {
                text = "<color=yellow>" + text + "</color>";
            }
            else if (type != LogType.Log) {
                text = "<color=red>" + text + "</color>";
            }
            textMessage.text = text;
        }

        private IEnumerator Start() {
            yield return new WaitForEndOfFrame();
            rectTransform.anchoredPosition = new Vector2(rectTransform.sizeDelta.x, -rectTransform.sizeDelta.y);
            rectTransform.DOAnchorPosX(0, 0.3f).SetEase(Ease.OutExpo);
            SyncPosition();
        }

        private void Update() {
            if (destroyed) {
                return;
            }
            duration -= Time.deltaTime;
            if (duration <= 0) {
                destroyed = true;
                rectTransform.DOAnchorPosX(rectTransform.sizeDelta.x, 0.3f).SetEase(Ease.OutExpo).OnComplete(Destroy);
            }
        }

        private void OnDestroy() {
            if (CommonUtil.IsQuitting) {
                return;
            }
            list.Remove(this);
        }

    }

}
